package com.seele.tigerwallet.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;

/**
 * @Author: 郑波
 * @Time: 2018/6/7 17:45
 * @Description: 一个Fragement的几类
 */
public abstract class BaseFragment extends Fragment {

    protected Context mContext;

    private View mView;

    @Override
    public void onAttach(Context context) {
        mContext = context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);//需要添加这行代码
        mView = inflater.inflate(getLayoutId(), container, false);

        initView();




        return mView;
    }


    protected abstract int getLayoutId();


    /**
     * 一个公共的Byid的接口
     *
     * @param id
     * @param <E>
     * @return
     */
    public final <E extends View> E $f(int id) {
        try {
            return (E) mView.findViewById(id);
        } catch (Exception ex) {
            Log.e("BaseFragment", "Could not cast View to concrete class.", ex);
            throw ex;
        }
    }

    protected abstract void initView();


    public void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(mContext, cls);
        mContext.startActivity(intent);
    }


    public void startActivity(Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (null != bundle) {
            intent.putExtras(bundle);
            mContext.startActivity(intent);
        }

    }
}
